package com.moetube.video.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moetube.video.dao.BarrageMapper;
import com.moetube.video.pojo.Barrage;
import com.moetube.video.service.BarrageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * 弹幕业务实现层
 *
 * @author ccr
 * @version 1.0
 */
@Service
public class BarrageServiceImpl implements BarrageService {

    @Autowired
    private BarrageMapper barrageMapper;

    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List<Barrage> findAll() {
        return barrageMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Barrage findById(Long id) {
        return barrageMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     *
     * @param barrage
     */
    @Override
    public void add(Barrage barrage) {
        barrageMapper.insert(barrage);
    }


    /**
     * 修改
     *
     * @param barrage
     */
    @Override
    public void update(Barrage barrage) {
        barrageMapper.updateByPrimaryKey(barrage);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        barrageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Barrage> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return barrageMapper.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Barrage> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Barrage>) barrageMapper.selectAll();
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Page<Barrage> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Barrage>) barrageMapper.selectByExample(example);
    }

    @Override
    public List<Barrage> findList(String videoId) {
        return barrageMapper.findListByVideoId(videoId);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Barrage.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // xid
            if (searchMap.get("xid") != null && !"".equals(searchMap.get("xid"))) {
                criteria.andLike("xid", "%" + searchMap.get("xid") + "%");
            }
            // ext
            if (searchMap.get("ext") != null && !"".equals(searchMap.get("ext"))) {
                criteria.andLike("ext", "%" + searchMap.get("ext") + "%");
            }

            // log_status
            if (searchMap.get("logStatus") != null) {
                criteria.andEqualTo("logStatus", searchMap.get("logStatus"));
            }

        }
        return example;
    }
}
